package cn.tedu.web.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import cn.tedu.web.entity.User;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author issen
 * @since 2021-09-17
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {
    /**
     * 检查邮箱是否存在
     */
    @Select("SELECT COUNT(1) FROM ec_user WHERE email = #{email}")
    public Boolean exists(String email);

    /**
     * 插入一条数据到 user 表
     */
    @Insert("INSERT INTO ec_user (`email`, `password`, `nickname`) VALUES (#{email}, MD5(#{password}), #{nickname});")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public int insert(User user);

    /**
     * 激活账号的状态
     * 
     * @param id 账号的ID
     */
    @Update("UPDATE `ec_user` SET `status` = 1 WHERE `id` = #{id}")
    public Integer activeStatus(@Param("id") Integer id);

    /**
	 * 根据邮箱和密码获取用户的ID
	 * @param email 邮箱
	 * @param password 密码
	 */
	@Select("SELECT id FROM ec_user WHERE email = #{e} AND `password` = MD5(#{p})")
	public Integer findUserByEmailAndPassword(@Param("e") String email, @Param("p") String password);

	/**
	 * 根据ID获取用户的昵称
	 * @param id 用户的ID
	 */
	@Select("SELECT nickname FROM ec_user WHERE id = #{id}")
	public String findNickNameById(@Param("id") Integer id);
}
